package com.boot.leetcode;

import jdk.nashorn.internal.objects.NativeUint8Array;

import java.util.HashSet;
import java.util.Set;

/**
 * @Package com.boot.leetcode
 */
public class ArrayTester {

    /**
     * 返回数组第k大的数字
     */

    public static int getTopK(int[] arr,int k){
        for (int i = 0; i < arr.length; i++) {
            for(int j=i;j>0;j--){
                if(arr[j-1]>arr[j]){
                    int tmp=arr[j];
                    arr[j]=arr[j-1];
                    arr[j-1]=tmp;
                }
            }
        }
        return arr[arr.length-k];
    }

    public static String minNumber(int[] nums) {
        String[] strs = new String[nums.length];
        for(int i = 0; i < nums.length; i++)
            strs[i] = String.valueOf(nums[i]);
        quickSort(strs, 0, strs.length - 1);
        StringBuilder res = new StringBuilder();
        for(String s : strs)
            res.append(s);
        return res.toString();
    }

    public static void quickSort(String[] strs, int l, int r) {
        if(l >= r) return;
        int i = l, j = r;
        String tmp = strs[i];
        while(i < j) {
            while((strs[j] + strs[l]).compareTo(strs[l] + strs[j]) >= 0 && i < j) j--;
            while((strs[i] + strs[l]).compareTo(strs[l] + strs[i]) <= 0 && i < j) i++;
            tmp = strs[i];
            strs[i] = strs[j];
            strs[j] = tmp;
        }
        strs[i] = strs[l];
        strs[l] = tmp;
        quickSort(strs, l, i - 1);
        quickSort(strs, i + 1, r);
    }

    public static Set<Integer> findOnceNums(int[] arr){
        Set<Integer> nums =new HashSet<>();
        for (int i = 0; i < arr.length; i++) {
            if(nums.contains(arr[i])){
                nums.remove(arr[i]);
                continue;
            }
            nums.add(arr[i]);
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] arr=new int[]{1,2,9,5,3,7,8,6,20,12};
        System.out.println(getTopK(arr,2));
        arr=new int[]{1,2,2,5,2,3,3,8};
        System.out.println(findOnceNums(arr));
    }

}
